﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using DaphneProfiBase;
using GameEngine;
using GalaSoft.MvvmLight.Messaging;


namespace DaphneControls.ViewModel
{
    /// <summary>
    /// Editor Window View Model
    /// </summary>
    public class PositionEditorVM : ViewModelBase
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public PositionEditorVM()
        {
            InEditMode = false;
            UndoButtonEnabled = false;
            RedoButtonEnabled = false;

            RegisterCommands();

            // register message from receiving the state from the checkersboard
            Messenger.Default.Register<NotificationMessage<EditorStateCommand>>(this, (nm) =>
                {
                    if (nm.Notification == Notify.SetEditorState)
                    {
                        HandleStateMessage(nm.Content);
                    }
                });
        }

        #region Binding Properties

        public bool InEditMode
        { get; set; }

        public bool UndoButtonEnabled
        { get; set; }

        public bool RedoButtonEnabled
        { get; set; }

        public bool WhitePlayerChecked
        { get; set; }

        public bool BlackPlayerChecked
        { get; set; }

        public string FEN
        { get; set; }

        #endregion

        #region Commands

        public RelayCommand StartEditCommand
        { get; set; }

        public RelayCommand ApproveCommand
        { get; set; }

        public RelayCommand CancelCommand
        { get; set; }

        public RelayCommand SetStartingPositionCommand
        { get; set; }

        public RelayCommand ClearBoardCommand
        { get; set; }

        public RelayCommand UndoCommand
        { get; set; }

        public RelayCommand RedoCommand
        { get; set; }

        public RelayCommand ParseFENCommand
        { get; set; }

        public RelayCommand SideToMoveCommand
        { get; set; }

        #endregion

        /// <summary>
        /// Registers commands and handles their events
        /// </summary>
        private void RegisterCommands()
        {
            StartEditCommand = new RelayCommand(() =>
                {
                    InEditMode = true;
                    RaisePropertyChanged("InEditMode");
                    SendNotification(Notify.StartEditing);
                });

            ApproveCommand = new RelayCommand(() =>
                {
                    InEditMode = false;
                    RaisePropertyChanged("InEditMode");
                    SendNotification(Notify.ApprovePosition);
                });

            CancelCommand = new RelayCommand(() =>
                {
                    InEditMode = false;
                    RaisePropertyChanged("InEditMode");
                    SendNotification(Notify.CancelEditing);
                });

            SetStartingPositionCommand = new RelayCommand(() =>
                {
                    SendNotification(Notify.SetStartPositon);
                });

            ClearBoardCommand = new RelayCommand(() =>
                {
                    SendNotification(Notify.ClearCheckersBoard);
                });

            UndoCommand = new RelayCommand(() =>
                {
                    SendNotification(Notify.PositionUndo);
                });

            RedoCommand = new RelayCommand(() =>
                {
                    SendNotification(Notify.PositionRedo);
                });

            ParseFENCommand = new RelayCommand(() =>
                {
                    SendNotification(Notify.ParseFEN);
                });

            SideToMoveCommand = new RelayCommand(() =>
                {
                    SendNotification(Notify.SetSideToMove);
                });
        }

        /// <summary>
        /// Sends the notification to the active checkers board
        /// </summary>
        /// <param name="notifyType"></param>
        private void SendNotification(string notifyType)
        {
            // create the editor notification container class
            EditorCommand cmd = new EditorCommand();
            cmd.SubCommand = notifyType;
            cmd.SideToMove = WhitePlayerChecked == true ? enumFigureColor.White : enumFigureColor.Black;
            cmd.FEN = FEN;

            // send the notification
            Messenger.Default.Send<NotificationMessage<EditorCommand>>(
                new NotificationMessage<EditorCommand>(cmd, Notify.EditorCommand));          
        }

        /// <summary>
        /// Handles the state message from the current checkers board
        /// </summary>
        private void HandleStateMessage(EditorStateCommand cmd)
        {
            InEditMode = cmd.IsInEditMode;
            UndoButtonEnabled = cmd.UndoEnabled;
            RedoButtonEnabled = cmd.RedoEnabled;
            if (cmd.SideToMove == enumFigureColor.White)
                WhitePlayerChecked = true;
            else
                BlackPlayerChecked = true;

            RaisePropertyChanged("InEditMode");
            RaisePropertyChanged("UndoButtonEnabled");
            RaisePropertyChanged("RedoButtonEnabled");
            RaisePropertyChanged("WhitePlayerChecked");
            RaisePropertyChanged("BlackPlayerChecked");
        }
    }
}